home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIFBG.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  12KB  |  363 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to generate back ground image that can be used to replace constant *
  7. * background.                                     *
  8. * Options:                                     *
  9. * -q : quite printing mode.                             *
  10. * -d direction : set direction image should increase intensity.             *
  11. * -l levels : number of color levels.                         *
  12. * -c r g b : colors of the back ground.                         *
  13. * -m min : minimin intensity in percent.                     *
  14. * -M max : maximum intensity in percent.                     *
  15. * -s width height : size of image to create.                     *
  16. * -h : on line help.                                 *
  17. ******************************************************************************
  18. * History:                                     *
  19. * 9 Jul 89 - Version 1.0 by Gershon Elber.                     *
  20. *****************************************************************************/
  21.  
  22. #ifdef __MSDOS__
  23. #include <stdlib.h>
  24. #include <alloc.h>
  25. #endif /* __MSDOS__ */
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include "gif_lib.h"
  31. #include "getarg.h"
  32.  
  33. #define PROGRAM_NAME    "GifBG"
  34.  
  35. #define DEFAULT_WIDTH    640
  36. #define DEFAULT_HEIGHT    350
  37.  
  38. #define DEFAULT_COLOR_RED    0
  39. #define DEFAULT_COLOR_GREEN    0
  40. #define DEFAULT_COLOR_BLUE    255
  41.  
  42. #define DEFAULT_MIN_INTENSITY    10                  /* In percent. */
  43. #define DEFAULT_MAX_INTENSITY    100
  44.  
  45. #define DEFAULT_NUM_LEVELS    16     /* Number of colors to gen the image. */
  46.  
  47. #define DIR_NONE    0         /* Direction the levels can be changed: */
  48. #define DIR_TOP        1
  49. #define DIR_TOP_RIGHT    2
  50. #define DIR_RIGHT    3
  51. #define DIR_BOT_RIGHT    4
  52. #define DIR_BOT        5
  53. #define DIR_BOT_LEFT    6
  54. #define DIR_LEFT    7
  55. #define DIR_TOP_LEFT    8
  56.  
  57. #define DEFAULT_DIR    "T"               /* TOP (North) direction. */
  58.  
  59. #ifdef __MSDOS__
  60. extern unsigned int
  61.     _stklen = 16384;                 /* Increase default stack size. */
  62. #endif /* __MSDOS__ */
  63.  
  64. #ifdef SYSV
  65. static char *VersionStr =
  66.         "Gif library module    \t\tGershon Elber\n\
  67.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  68. static char
  69.     *CtrlStr = "GifBG q%- d%-Dir!s l%-#Lvls!d c%-R|G|B!d!d!d m%-MinI!d M%-MaxI!d s%-W|H!d!d h%-";
  70. #else
  71. static char
  72.     *VersionStr =
  73.     PROGRAM_NAME
  74.     GIF_LIB_VERSION
  75.     "    Gershon Elber,    "
  76.     __DATE__ ",   " __TIME__ "\n"
  77.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  78. static char
  79.     *CtrlStr =
  80.     PROGRAM_NAME
  81.     " q%- d%-Dir!s l%-#Lvls!d c%-R|G|B!d!d!d m%-MinI!d M%-MaxI!d s%-W|H!d!d h%-";
  82. #endif /* SYSV */
  83.  
  84. static int
  85.     MaximumIntensity = DEFAULT_MAX_INTENSITY,              /* In percent. */
  86.     MinimumIntensity = DEFAULT_MIN_INTENSITY,
  87.     NumLevels = DEFAULT_NUM_LEVELS,
  88.     ImageWidth = DEFAULT_WIDTH,
  89.     ImageHeight = DEFAULT_HEIGHT,
  90.     Direction;
  91. static unsigned int
  92.     RedColor = DEFAULT_COLOR_RED,
  93.     GreenColor = DEFAULT_COLOR_GREEN,
  94.     BlueColor = DEFAULT_COLOR_BLUE;
  95.  
  96. static void QuitGifError(GifFileType *GifFile);
  97.  
  98. /******************************************************************************
  99. * Interpret the command line and scan the given GIF file.              *
  100. ******************************************************************************/
  101. void main(int argc, char **argv)
  102. {
  103.     unsigned int Ratio;
  104.     int    i, j, l, LevelHeight, LevelWidth, Error, LogNumLevels, FlipDir,
  105.     Accumulator, StartX, StepX, Count = 0, DoAllMaximum = FALSE,
  106.     DirectionFlag = FALSE, LevelsFlag = FALSE, ColorFlag = FALSE,
  107.     MinFlag = FALSE, MaxFlag = FALSE, SizeFlag = FALSE, HelpFlag = FALSE;
  108.     GifPixelType Color;
  109.     char *DirectionStr = DEFAULT_DIR;
  110.     GifRowType Line;
  111.     GifColorType *ColorMap;
  112.     GifFileType *GifFile;
  113.  
  114.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  115.         &DirectionFlag, &DirectionStr, &LevelsFlag, &NumLevels,
  116.         &ColorFlag, &RedColor, &GreenColor, &BlueColor,
  117.         &MinFlag, &MinimumIntensity, &MaxFlag, &MaximumIntensity,
  118.         &SizeFlag, &ImageWidth, &ImageHeight,
  119.         &HelpFlag)) != FALSE) {
  120.     GAPrintErrMsg(Error);
  121.     GAPrintHowTo(CtrlStr);
  122.     exit(1);
  123.     }
  124.  
  125.     if (HelpFlag) {
  126.     fprintf(stderr, VersionStr);
  127.     GAPrintHowTo(CtrlStr);
  128.     exit(0);
  129.     }
  130.  
  131.     /* Make sure intensities are in the right range: */
  132.     if (MinimumIntensity < 0 || MinimumIntensity > 100 ||
  133.     MaximumIntensity < 0 || MaximumIntensity > 100)
  134.     GIF_EXIT("Intensities (-m or -M options) are not in [0..100] range (percent).");
  135.  
  136.     /* Convert DirectionStr to our local representation: */
  137.     Direction = DIR_NONE;
  138.     FlipDir = FALSE;
  139.     for (i = 0; i < strlen(DirectionStr);  i++) /* Make sure its upper case. */
  140.     if (islower(DirectionStr[i]))
  141.         DirectionStr[i] = toupper(DirectionStr[i]);
  142.  
  143.     switch(DirectionStr[0]) {
  144.     case 'T': /* Top or North */
  145.     case 'N':
  146.         if (strlen(DirectionStr) < 2)
  147.         Direction = DIR_TOP;
  148.         else
  149.         switch(DirectionStr[1]) {
  150.             case 'R':
  151.             case 'E':
  152.             Direction = DIR_TOP_RIGHT;
  153.             break;
  154.             case 'L':
  155.             case 'W':
  156.             Direction = DIR_TOP_LEFT;
  157.             FlipDir = TRUE;
  158.             break;
  159.         }
  160.         break;
  161.     case 'R': /* Right or East */
  162.     case 'E':
  163.         Direction = DIR_RIGHT;
  164.         break;
  165.     case 'B': /* Bottom or South */
  166.     case 'S':
  167.         if (strlen(DirectionStr) < 2) {
  168.         Direction = DIR_BOT;
  169.         FlipDir = TRUE;
  170.         }
  171.         else
  172.         switch(DirectionStr[1]) {
  173.             case 'R':
  174.             case 'E':
  175.             Direction = DIR_BOT_RIGHT;
  176.             break;
  177.             case 'L':
  178.             case 'W':
  179.             Direction = DIR_BOT_LEFT;
  180.             FlipDir = TRUE;
  181.             break;
  182.         }
  183.         break;
  184.     case 'L': /* Left or West */
  185.     case 'W':
  186.         Direction = DIR_LEFT;
  187.         FlipDir = TRUE;
  188.         break;
  189.     }
  190.     if (Direction == DIR_NONE)
  191.     GIF_EXIT("Direction requested (-d option) is wierd!");
  192.  
  193.     /* We are going to handle only TOP, TOP_RIGHT, RIGHT, BOT_RIGHT  so flip */
  194.     /* the complement cases (TOP <-> BOT for example) by flipping the         */
  195.     /* Color i with color (NumLevels - i - 1).                     */
  196.     if (FlipDir) {
  197.     switch (Direction) {
  198.         case DIR_BOT:
  199.         Direction = DIR_TOP;
  200.         break;
  201.         case DIR_BOT_LEFT:
  202.         Direction = DIR_TOP_RIGHT;
  203.         break;
  204.         case DIR_LEFT:
  205.         Direction = DIR_RIGHT;
  206.         break;
  207.         case DIR_TOP_LEFT:
  208.         Direction = DIR_BOT_RIGHT;
  209.         break;
  210.     }
  211.     }
  212.  
  213.     /* If binary mask is requested (special case): */
  214.     if (MinimumIntensity == 100 && MaximumIntensity == 100 && NumLevels == 2) {
  215.     MinimumIntensity = 0;
  216.     DoAllMaximum = TRUE;
  217.     Direction = DIR_RIGHT;
  218.     }
  219.  
  220.     /* Make sure colors are in the right range: */
  221.     if (RedColor > 255 || GreenColor > 255 || BlueColor > 255)
  222.     GIF_EXIT("Colors are not in the ragne [0..255].");
  223.  
  224.     /* Make sure number of levels is power of 2 (up to 8 bits per pixel).    */
  225.     for (i = 1; i < 8; i++) if (NumLevels == (1 << i)) break;
  226.     if (i == 8) GIF_EXIT("#Lvls (-l option) is not power of 2.");
  227.     LogNumLevels = i;
  228.  
  229.     /* Open stdout for the output file: */
  230.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  231.     QuitGifError(GifFile);
  232.  
  233.     /* Dump out screen description with given size and generated color map:  */
  234.     if ((ColorMap = (GifColorType *) malloc(NumLevels * sizeof(GifColorType)))
  235.     == NULL) GIF_EXIT("Failed to allocate memory required, aborted.");
  236.  
  237.     for (i = 1; i <= NumLevels; i++) {
  238.     /* Ratio will be in the range of 0..100 for required intensity: */
  239.     Ratio = (MaximumIntensity * (i * (256 / NumLevels)) +
  240.          MinimumIntensity * ((NumLevels - i) * (256 / NumLevels))) /
  241.          256;
  242.     ColorMap[i-1].Red   = (RedColor * Ratio) / 100;
  243.     ColorMap[i-1].Green = (GreenColor * Ratio) / 100;
  244.     ColorMap[i-1].Blue  = (BlueColor * Ratio) / 100;
  245.     }
  246.     if (EGifPutScreenDesc(GifFile,
  247.     ImageWidth, ImageHeight, LogNumLevels, 0, LogNumLevels, ColorMap)
  248.     == GIF_ERROR)
  249.     QuitGifError(GifFile);
  250.  
  251.     /* Dump out the image descriptor: */
  252.     if (EGifPutImageDesc(GifFile,
  253.     0, 0, ImageWidth, ImageHeight, FALSE, LogNumLevels, NULL) == GIF_ERROR)
  254.     QuitGifError(GifFile);
  255.  
  256.     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  257.            PROGRAM_NAME, GifFile -> ILeft, GifFile -> ITop,
  258.            GifFile -> IWidth, GifFile -> IHeight);
  259.  
  260.     /* Allocate one scan line twice as big as image is as we are going to    */
  261.     /* shift along it, while we dump the scan lines:                 */
  262.     if ((Line = (GifRowType) malloc(sizeof(GifPixelType) * ImageWidth * 2)) == NULL)
  263.     GIF_EXIT("Failed to allocate memory required, aborted.");
  264.  
  265.     if (Direction == DIR_TOP) {
  266.     /* We must evaluate the line each time level is changing: */
  267.     LevelHeight = ImageHeight / NumLevels;
  268.     for (Color = NumLevels, i = l = 0; i < ImageHeight; i++) {
  269.         if (i == l) {
  270.         /* Time to update the line to next color level: */
  271.         if (Color != 0) Color--;
  272.         for (j = 0; j < ImageWidth; j++)
  273.             Line[j] = (FlipDir ? NumLevels - Color - 1 : Color);
  274.         l += LevelHeight;
  275.         }
  276.         if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
  277.         QuitGifError(GifFile);
  278.         GifQprintf("\b\b\b\b%-4d", Count++);
  279.     }
  280.     }
  281.     else if (Direction == DIR_RIGHT) {
  282.     /* We pre-prepare the scan lines as going from color zero to maximum */
  283.     /* color and dump the same scan line Height times:             */
  284.     /* Note this case should handle the Boolean Mask special case.         */
  285.     LevelWidth = ImageWidth / NumLevels;
  286.     if (DoAllMaximum) {
  287.         /* Special case - do all in maximum color: */
  288.         for (i = 0; i < ImageWidth; i++) Line[i] = 1;
  289.     }
  290.     else {
  291.         for (Color = i = 0, l = LevelWidth; i < ImageWidth; i++, l--) {
  292.         if (l == 0) {
  293.             l = LevelWidth;
  294.             if (Color < NumLevels - 1) Color++;
  295.         }
  296.         Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
  297.         }
  298.     }
  299.  
  300.     for (i = 0; i < ImageHeight; i++) {
  301.         if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
  302.         QuitGifError(GifFile);
  303.         GifQprintf("\b\b\b\b%-4d", Count++);
  304.     }
  305.     }
  306.     else {
  307.     /* We are in one of the TOP_RIGHT, BOT_RIGHT cases: we will          */
  308.     /* initialize the Line with its double ImageWidth length from the    */
  309.     /* minimum intensity to the maximum intensity and shift along it     */
  310.     /* while we go along the image height.                     */
  311.     LevelWidth = ImageWidth * 2 / NumLevels;
  312.     for (Color = i = 0, l = LevelWidth; i < ImageWidth * 2; i++, l--) {
  313.         if (l == 0) {
  314.         l = LevelWidth;
  315.         if (Color < NumLevels - 1) Color++;
  316.         }
  317.         Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
  318.     }
  319.     /* We need to implement a DDA to know how much to shift Line while   */
  320.     /* we go down along image height. we set the parameters for it now:  */
  321.     Accumulator = 0;
  322.     switch(Direction) {
  323.         case DIR_TOP_RIGHT:
  324.         StartX = ImageWidth;
  325.         StepX = -1;
  326.         break;
  327.         case DIR_BOT_RIGHT:
  328.         default:
  329.         StartX = 0;
  330.         StepX = 1;
  331.         break;
  332.     }
  333.  
  334.     /* Time to dump information out: */
  335.     for (i = 0; i < ImageHeight; i++) {
  336.         if (EGifPutLine(GifFile, &Line[StartX], ImageWidth) == GIF_ERROR)
  337.         QuitGifError(GifFile);
  338.         GifQprintf("\b\b\b\b%-4d", Count++);
  339.         if ((Accumulator += ImageWidth) > ImageHeight) {
  340.         while (Accumulator > ImageHeight) {
  341.             Accumulator -= ImageHeight;
  342.             StartX += StepX;
  343.         }
  344.         if (Direction < 0) Direction = 0;
  345.         if (Direction > ImageWidth) Direction = ImageWidth;
  346.         }
  347.     }
  348.     }
  349.  
  350.     if (EGifCloseFile(GifFile) == GIF_ERROR)
  351.     QuitGifError(GifFile);
  352. }
  353.  
  354. /******************************************************************************
  355. * Close output file (if open), and exit.                      *
  356. ******************************************************************************/
  357. static void QuitGifError(GifFileType *GifFile)
  358. {
  359.     PrintGifError();
  360.     if (GifFile != NULL) EGifCloseFile(GifFile);
  361.     exit(1);
  362. }
  363.